/** 默认缓存期限为7天 */
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;

/**
 * 设置localStorage
 * @param key
 * @param value
 * @param expire
 */
export function setLocal(key, value, expire = DEFAULT_CACHE_TIME) {
  const storageData = {
    value,
    expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
  };
  const json = JSON.stringify(storageData);
  window.localStorage.setItem(key, json);
}
/**
 * 获取localStorage
 * @param key
 */
export function getLocal(key) {
  const json = window.localStorage.getItem(key);
  if (json) {
    let storageData = null;
    try {
      storageData = JSON.parse(json);
    } catch {
      // 防止解析失败中断程序
    }
    if (storageData) {
      const { value, expire } = storageData;
      // 在有效期内直接返回
      if (expire === null || expire >= Date.now()) {
        return value;
      }
    }
    removeLocal(key);
    return null;
  }
  return null;
}

export function removeLocal(key) {
  window.localStorage.removeItem(key);
}

export function clearLocal() {
  window.localStorage.clear();
}
